//
//  OrderDetailBottomView.m
//  Yanwei
//
//  Created by 刘文 on 17/6/9.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "OrderDetailBottomView.h"

const CGFloat kBootomLineWidth = 0.5;

@interface OrderDetailBottomView ()

@property (strong, nonatomic) UILabel * payMoneyLabel;
@property (strong, nonatomic) UIButton * cancelOrderBtn;//取消订单 左边按钮
@property (strong, nonatomic) UIButton * payButton;//支付按钮 右边按钮

@end

@implementation OrderDetailBottomView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, ScreenWidth, 49);
    }
    return self;
}

- (void)setModel:(Orders *)model {
    _model = model;
    
    /**
    if (model.orderTypeState == OrderTypeTicket) {
        self.payMoneyLabel.text = [NSString stringWithFormat:@"%@ ", model.amount];
    } else {
        self.payMoneyLabel.text = [NSString stringWithFormat:@"%@ ", model.realAmount];
    }
    
    self.model.drawbackEnable = NO;
     */
    [self updateBtnState];
}

- (void)updateBtnState {
    /**
    if (self.model.orderTypeState == OrderTypeTicket) {
        [self updateBtnStateForTicketOrder];
    } else if (self.model.orderTypeState == OrderTypeSolicitGoods) {
        [self updateBtnStateForSolicitGoods];
    } else {
        [self updateBtnStateForOrdinaryGoods];
    }
     */
}

- (void)updateBtnStateForSolicitGoods {
    /**
    switch (self.model.orderStateIndex) {
        case OrderStateProcessing:
        case OrderStatePayPending: {
            self.payButton.tag = FooterViewBtnTypePayOrder;
            [self.payButton setTitle:@"付款" forState:UIControlStateNormal];
            [self.payButton roundBorderWithBorderDirection:BorderDirectionALL lineWidth:kBootomLineWidth color:Color175030];
            self.payButton.hidden = NO;
            
            self.cancelOrderBtn.tag = FooterViewBtnTypeCancelOrder;
            [self.cancelOrderBtn setTitle:@"取消订单" forState:UIControlStateNormal];
            [self.cancelOrderBtn setTitleColor:Color140 forState:UIControlStateNormal];
            [self.cancelOrderBtn roundBorderWithBorderDirection:BorderDirectionALL lineWidth:kBootomLineWidth color:[UIColor whiteColor]];
            self.cancelOrderBtn.hidden = NO;
            break;
        }
        case OrderStateReceivePending: {
            self.payButton.tag = FooterViewBtnTypeHasReceived;
            [self.payButton setTitle:@"确认收货" forState:UIControlStateNormal];
            self.payButton.hidden = NO;
            
            self.cancelOrderBtn.tag = FooterViewBtnTypeApplyRefund;
            [self.cancelOrderBtn setTitle:@"申请退款" forState:UIControlStateNormal];
            self.cancelOrderBtn.hidden = YES;
            break;
        }
        case OrderStateDeliveryPending: {
            self.payButton.tag = FooterViewBtnTypeApplyRefund;
            [self.payButton setTitle:@"申请退款" forState:UIControlStateNormal];
            self.payButton.hidden = YES;
            
            self.cancelOrderBtn.hidden = YES;
            break;
        }
        case OrderStateSuccess: {
            [self.payButton setTitle:@"删除" forState:UIControlStateNormal];
            self.payButton.tag = FooterViewBtnTypeDeteleOrder;
            self.payButton.hidden = YES;
            
            self.cancelOrderBtn.tag = FooterViewBtnTypeApplyRefund;
            [self.cancelOrderBtn setTitle:@"申请退款" forState:UIControlStateNormal];
            self.cancelOrderBtn.hidden = !self.model.drawbackEnable;
            self.cancelOrderBtn.hidden = YES;
            [self.cancelOrderBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.payButton).priorityHigh();
            }];
            break;
        }
            
        case OrderStateRefundDenied:
        case OrderStateRefundSuccess:
        case OrderStateRefundPending:
            break;
            
        case OrderStateInvalid: {
            [self.payButton setTitle:@"删除" forState:UIControlStateNormal];
            self.payButton.tag = FooterViewBtnTypeDeteleOrder;
            self.payButton.hidden = YES;
            
            self.cancelOrderBtn.hidden = YES;
            break;
        }
            
        default: {
            self.payButton.tag = FooterViewBtnTypeNoraml;
            self.payButton.hidden = YES;
            self.cancelOrderBtn.tag = FooterViewBtnTypeNoraml;
            self.cancelOrderBtn.hidden = YES;
            break;
        }
    }
     */
}

/** 通用商品底部按钮状态 */
- (void)updateBtnStateForOrdinaryGoods {
    /**
    switch (self.model.orderStateIndex) {
        case OrderStateProcessing:
        case OrderStatePayPending: {
            self.payButton.tag = FooterViewBtnTypePayOrder;
            [self.payButton setTitle:@"付款" forState:UIControlStateNormal];
            self.payButton.hidden = NO;
            
            self.cancelOrderBtn.tag = FooterViewBtnTypeCancelOrder;
            [self.cancelOrderBtn setTitle:@"取消订单" forState:UIControlStateNormal];
            [self.cancelOrderBtn setTitleColor:Color140 forState:UIControlStateNormal];
            [self.cancelOrderBtn roundBorderWithBorderDirection:BorderDirectionALL lineWidth:kBootomLineWidth color:[UIColor whiteColor]];
            self.cancelOrderBtn.hidden = NO;
            break;
        }
        case OrderStateReceivePending: {
            self.payButton.tag = FooterViewBtnTypeHasReceived;
            [self.payButton setTitle:@"确认收货" forState:UIControlStateNormal];
            self.payButton.hidden = NO;
            
            self.cancelOrderBtn.tag = FooterViewBtnTypeApplyRefund;
            [self.cancelOrderBtn setTitle:@"申请退款" forState:UIControlStateNormal];
            self.cancelOrderBtn.hidden = !self.model.drawbackEnable;
            break;
        }
        case OrderStateDeliveryPending: {
            if (self.model.orderListOrderAuditStatType == OrderListOrderAuditStatTypePass) {
                self.payButton.tag = FooterViewBtnTypeNoraml;
                self.payButton.hidden = YES;
                self.cancelOrderBtn.tag = FooterViewBtnTypeNoraml;
                self.cancelOrderBtn.hidden = YES;
            } else {
                self.payButton.tag = FooterViewBtnTypeApplyRefund;
                [self.payButton setTitle:@"申请退款" forState:UIControlStateNormal];
                self.payButton.hidden = !self.model.drawbackEnable;
                
                self.cancelOrderBtn.hidden = YES;
            }
            break;
        }
        case OrderStateSuccess: {
            [self.payButton setTitle:@"删除" forState:UIControlStateNormal];
            self.payButton.tag = FooterViewBtnTypeDeteleOrder;
            self.payButton.hidden = YES;
            
            self.cancelOrderBtn.tag = FooterViewBtnTypeApplyRefund;
            [self.cancelOrderBtn setTitle:@"申请退款" forState:UIControlStateNormal];
            self.cancelOrderBtn.hidden = !self.model.drawbackEnable;
            [self.cancelOrderBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.payButton).priorityHigh();
            }];
            break;
        }
            
        case OrderStateRefundDenied:
        case OrderStateRefundSuccess:
        case OrderStateRefundPending:
            break;
            
        case OrderStateInvalid: {
            [self.payButton setTitle:@"删除" forState:UIControlStateNormal];
            self.payButton.tag = FooterViewBtnTypeDeteleOrder;
            self.payButton.hidden = YES;
            
            self.cancelOrderBtn.hidden = YES;
            break;
        }
            
        default: {
            self.payButton.tag = FooterViewBtnTypeNoraml;
            self.payButton.hidden = YES;
            self.cancelOrderBtn.tag = FooterViewBtnTypeNoraml;
            self.cancelOrderBtn.hidden = YES;
            break;
        }
    }
     */
}

/** 门票订单的状态 */
- (void)updateBtnStateForTicketOrder {
    // 门票订单必显示查看卡券
    self.payButton.hidden = NO;
    self.payButton.tag = FooterViewBtnTypeCheckCoupon;
    [self.payButton setTitle:@"查看卡券" forState:UIControlStateNormal];
    self.cancelOrderBtn.hidden = YES;
    
    /**
    switch (self.model.orderStateIndex) {
        case OrderStateProcessing:
        case OrderStatePayPending: {
            self.payButton.tag = FooterViewBtnTypePayOrder;
            [self.payButton setTitle:@"立即支付" forState:UIControlStateNormal];
            self.payButton.hidden = NO;
            break;
        }
        case OrderStateSuccess: {
            if (self.model.drawbackEnable) { // 订单成功且可退款则显示
                self.cancelOrderBtn.tag = FooterViewBtnTypeApplyRefund;
                [self.cancelOrderBtn setTitle:@"申请退款" forState:UIControlStateNormal];
                self.cancelOrderBtn.hidden = NO;
            }
        }
        default: {
            break;
        }
    }
     */
}

- (void)btnAction:(UIButton *)sender {
    if (self.orderDetailBottomViewBlock) {
        self.orderDetailBottomViewBlock(sender.tag);
    }
}

- (UILabel *)payMoneyLabel {
    if (!_payMoneyLabel) {
        UILabel * lab = [UILabel new];
        lab.font = APPFONT(15);
        lab.text = @"实付：";
        lab.textColor = HEXCOLOR(0x000000);
        lab.backgroundColor = [UIColor clearColor];
        [self addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(18);
            make.centerY.equalTo(self);
        }];

        _payMoneyLabel = [UILabel new];
        _payMoneyLabel.font = APPBOLDPINGFANGFONT(15);
        _payMoneyLabel.textColor = Color175030;
        _payMoneyLabel.backgroundColor = [UIColor clearColor];
        _payMoneyLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_payMoneyLabel];
        [_payMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lab.mas_right);
            make.centerY.equalTo(lab);
            make.width.mas_lessThanOrEqualTo(@200);
        }];
    }
    return _payMoneyLabel;
}

- (UIButton *)payButton {
    if (!_payButton) {
        _payButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _payButton.titleLabel.font = APPFONT(12);
        [_payButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _payButton.layer.cornerRadius = 13;
        _payButton.layer.masksToBounds = YES;
        [_payButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_payButton];
        [_payButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(-18);
            make.width.mas_equalTo(@66);
            make.height.mas_equalTo(@26);
            make.centerY.equalTo(self);
        }];
        _payButton.backgroundColor = Color175030;
    }
    return _payButton;
}

- (UIButton *)cancelOrderBtn {
    if (!_cancelOrderBtn) {
        _cancelOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelOrderBtn.titleLabel.font = APPFONT(12);
        _cancelOrderBtn.titleLabel.textColor = HEXCOLOR(0x000000);
        _cancelOrderBtn.cornerRadius = 11;
        [_cancelOrderBtn setTitle:@"取消订单" forState:UIControlStateNormal];
        [_cancelOrderBtn setTitleColor:Color34 forState:UIControlStateNormal];
        [_cancelOrderBtn roundBorderWithBorderDirection:BorderDirectionALL lineWidth:kBootomLineWidth color:Color140];
        [_cancelOrderBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelOrderBtn];
        [_cancelOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.payButton.mas_left).with.offset(-10).priorityLow();
            make.size.equalTo(self.payButton);
            make.centerY.equalTo(self.payButton);
        }];
    }
    return _cancelOrderBtn;
}

@end
